<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\BaseController;
use App\Models\Client;
use App\Models\Event;
use App\Models\EventEmployee;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class EmployeesController extends BaseController
{
    /**
     * 获取所有的员工 /employee
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function getEmployeesList(){
        //查询所有员工信息
        $employees_data = User::where('is_admin', 1)->get(['id', 'email', 'photo', 'scope', 'firstname', 'lastname']);

        foreach ($employees_data as $value){
            $value['scope'] = $value['scope'] == 0 ? "course" : "training";
        }

        return $this->returnJson(BaseController::OK, 'success', $employees_data);
    }

    /**
     * 通过ID获取员工 /employee/{employee_id}
     * @param $employee_id
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function getEmployeesInfo($employee_id){
        $employee_data = User::where([
            'id'        => $employee_id,
            'is_admin'  =>  1
        ])->first(['id', 'email', 'photo', 'scope', 'firstname', 'lastname']);

        if (!$employee_data){
            return $this->returnJson(BaseController::NOTFOUND, 'not found');
        }

        return $this->returnJson(BaseController::OK, 'success', $employee_data);
    }

    /**
     * 删除员工 /employee/{employee_id}
     * @param $employee_id
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function deleteEmployees($employee_id, Request $request){
        //判断管理员是否删除自己
        if ($employee_id == $request->cache_data['id']){
            return $this->returnJson(BaseController::UNAUTHORIZED, 'unauthorized');
        }

        //删除员工或管理员
        $data = User::where('id', $employee_id)->delete();

        //判断删除的用户是否不存在
        if (!$data){
            return $this->returnJson(BaseController::NOTFOUND, 'not found');
        }

        //删除用户对应的事件关联
        EventEmployee::where('user_id', $employee_id)->delete();

        return $this->returnJson(BaseController::OK, 'success');
    }

    /**
     * 添加新员工 /employee
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function addEmployee(Request $request){
        $params = $request->only('email', 'scope', 'password', 'firstname', 'lastname', 'is_admin', 'photo');

        //判断为空
        if (empty($params['email']) || empty($params['scope']) || empty($params['password']) || empty($params['firstname'])
            || empty($params['lastname']) || empty($params['is_admin'] || empty($params['photo']))){
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'data cannot be processed');
        }

        //判断邮箱是否存在
        $is_email_exists = User::where('email', $params['email'])->first();
        if ($is_email_exists){
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'This email address has already been taken');
        }

        //上传图片
        if (!$url = $this->uploadFile(Request::capture(), 'photo')) {
            return $this->returnJson(BaseController::REQUIRED_FIELD_EMPTY, 'data cannot be processed');
        }

        $params['photo'] = $_SERVER['HTTP_HOST'] . '/' . $url;

        $id = User::insertGetId($params);

        return $this->returnJson(BaseController::OK, 'success', $id);
    }

}
